const { util } = require("utils");
const GZHConfig = require("../gzhConfig");
const crypto = require("crypto");
function paramCheck(params, httpInfo) {
    let { body: thisBody, isBase64Encoded } = httpInfo || {};
    let reqBody = thisBody;
    if (!!isBase64Encoded) {
        // 转utf8
        reqBody = Buffer.from(thisBody, "base64").toString();
    }
    // 解析body
    reqBody = util.convertBodyObj(reqBody, httpInfo.headers) || {};
    console.log("解析出的body是 ", reqBody);
    let Encrypt = null;
    if (reqBody.xml) {
        Encrypt = reqBody.xml.Encrypt;
    } else {
        Encrypt = reqBody.Encrypt;
    }
    // 检查参数
    let { timestamp, nonce, msg_signature } = params || {};
    if (
        util.nilValue(Encrypt) ||
        util.nilValue(timestamp) ||
        util.nilValue(nonce) ||
        util.nilValue(msg_signature)
    ) {
        return { flag: false, encrypt: null };
    }
    // 这个 要 sha1
    let needSign = [GZHConfig.verifyToken, timestamp, nonce, Encrypt].sort().join("");
    let hash = crypto.createHash("sha1").update(needSign).digest("hex");
    // 判断 和 参数中的 signature 是否一样
    if (hash !== msg_signature) {
        return { flag: false, encrypt: null };
    }
    return { flag: true, encrypt: Encrypt };
}

module.exports = {
    paramCheck,
};
